package cn.shop.admin.service.impl;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import cn.shop.admin.entity.backRoleModule.BackRole;
import cn.shop.admin.mapper.BackRoleMapper;
import cn.shop.admin.req.AssignRoleReq;
import cn.shop.admin.service.BackRoleService;
import cn.shop.base.MyServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色表(BackRole)表服务实现类
 *
 * @author jzw
 * @since 2021-11-17 11:00:28
 */
@Service("backRoleService")
public class BackRoleServiceImpl extends MyServiceImpl<BackRoleMapper, BackRole> implements BackRoleService {

	/**
	 * 批量设置默认角色
	 *
	 * @param idList 角色id集合
	 * @return
	 * @author jy
	 * @since 2022/1/17 15:32
	 */
	@Override
	public boolean setDefaultRole(List<Long> idList) {
		if (idList.size() > 0) {
			//去除所有默认角色
			LambdaUpdateWrapper<BackRole> updateWrapper = Wrappers.lambdaUpdate();
			updateWrapper.eq(BackRole::getDef, true).set(BackRole::getDef, false);
			BackRole entity = new BackRole();
			baseMapper.update(entity, updateWrapper);
			//重新分配默认角色
			LambdaUpdateWrapper<BackRole> updateInitWrapper = Wrappers.lambdaUpdate();
			updateInitWrapper.in(BackRole::getId, idList).set(BackRole::getDef, true);
			return SqlHelper.retBool(baseMapper.update(entity, updateInitWrapper));
		}
		return false;
	}

	/**
	 * 批量设置内置角色
	 *
	 * @param idList 角色id集合
	 * @return
	 * @author jy
	 * @since 2022/1/17 15:29
	 */
	@Override
	public boolean setInitRole(List<Long> idList) {
		if (idList.size() > 0) {
			//去除所有内置角色
			LambdaUpdateWrapper<BackRole> updateWrapper = Wrappers.lambdaUpdate();
			updateWrapper.eq(BackRole::getInitial, true).set(BackRole::getInitial, false);
			BackRole entity = new BackRole();
			baseMapper.update(entity, updateWrapper);
			//重新分配内置角色
			LambdaUpdateWrapper<BackRole> updateInitWrapper = Wrappers.lambdaUpdate();
			updateInitWrapper.in(BackRole::getId, idList).set(BackRole::getInitial, true);
			return SqlHelper.retBool(baseMapper.update(entity, updateInitWrapper));
		}
		return false;
	}

	/**
	 * 获取全部默认角色id
	 *
	 * @return
	 * @author jy
	 * @since 2022/1/17 15:40
	 */
	@Override
	public List<Object> listDefaultRole() {
		LambdaQueryWrapper<BackRole> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(BackRole::getDef, true).eq(BackRole::getEnable, true);
		queryWrapper.select(BackRole::getId);
		return listObjs(queryWrapper);
	}

	/**
	 * 获取用户关联的角色
	 *
	 * @param id 账户id
	 * @return
	 * @author jy
	 * @since 2022/1/17 15:18
	 */
	@Override
	public List<Long> getRoleByUserId(Long id) {
		return baseMapper.getRoleByUserId(id);

	}

	/**
	 * 分配角色
	 *
	 * @param assignRoleReq 实体对象
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean assignRole(AssignRoleReq assignRoleReq) {
		baseMapper.removeRoleByUserId(assignRoleReq.getUserId());
		boolean res = SqlHelper.retBool(baseMapper.assignRole(assignRoleReq));
		if (res) {
			//删除该用户登录的角色权限
			SaSession session = StpUtil.getSessionByLoginId(assignRoleReq.getUserId(), false);
			if (null != session) {
				session.delete("Role_List");
			}
		}
		return res;
	}

	/**
	 * 通过角色code查询角色的所有权限
	 *
	 * @param code 角色code
	 * @return 权限code
	 * @author jzw
	 * @since 2021/11/17 17:12
	 */
	@Override
	public List<String> permissionCodeByRoleCode(String code) {
		return baseMapper.permissionCodeByRoleCode(code);
	}

	/**
	 * 新增数据
	 *
	 * @param backRole 实体对象
	 * @return 新增结果
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@Override
	public boolean insert(BackRole backRole) {
		LambdaQueryWrapper<BackRole> query = Wrappers.lambdaQuery();
		query.eq(BackRole::getCode, backRole.getCode());
		BackRole role = baseMapper.selectOne(query);
		Assert.isNull(role, "code已存在");
		backRole.setEnable(true);
		backRole.setInitial(false);
		return SqlHelper.retBool(baseMapper.insert(backRole));
	}

	/**
	 * 修改数据
	 *
	 * @param backRole 实体对象
	 * @return 修改结果
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@Override
	public boolean update(BackRole backRole) {
		LambdaQueryWrapper<BackRole> query = Wrappers.lambdaQuery();
		query.eq(BackRole::getCode, backRole.getCode()).ne(BackRole::getId, backRole.getId());
		BackRole role = baseMapper.selectOne(query);
		Assert.isNull(role, "code已存在");
		return SqlHelper.retBool(baseMapper.updateById(backRole));
	}

	/**
	 * 通过角色id查询角色所有权限
	 *
	 * @param id 角色id
	 * @return 所有数据
	 * @author jzw
	 * @since 2021-11-17 11:33:20
	 */
	@Override
	public List<Long> queryPermissionsByRoleId(Long id) {
		return baseMapper.queryPermissionsByRoleId(id);
	}


}

